﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Space.Library;
using Space.Model.Data;

namespace Space.Model.Entity
{
    class StarField : EntityBase, IPaint
    {
        private readonly int _speed;
        readonly List<PointF> _stars = new List<PointF>();

        private readonly SolidBrush _brush;

        public StarField(Color color, int speed)
        {
            _speed = speed;
            _brush = new SolidBrush(color);
        }


        public override void Init(GameRunner runner)
        {
            base.Init(runner);

            for (int i = 0; i < 50; i++)
            {
                _stars.Add(new Point(runner.Random.Next(Board.Width), runner.Random.Next(Board.Height)));
            }

        }

        public void Paint(Painter painter, GameRunner runner)
        {
            foreach (var tuple in _stars)
            {
                painter.PutPixel(tuple, _brush);
            }
        }


        public override void Tick(GameRunner runner, int timestep)
        {
            base.Tick(runner, timestep);

            for (int i = 0; i < _stars.Count; i++)
            {
                var a = _stars[i];

                a.X += (float)timestep / (_speed * 10);

                if (a.X >= Board.Width)
                    a.X = 0;

                _stars[i] = a;
            }
        }

        public override void Cleanup(GameRunner runner)
        {
            base.Cleanup(runner);
        }
    }
}
